package com.jeecms.wicool.dao.car.impl;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import com.jeecms.cms.entity.main.CmsUser;
import com.jeecms.common.hibernate3.Finder;
import com.jeecms.common.hibernate3.HibernateBaseDao;
import com.jeecms.common.page.Pagination;
import com.jeecms.wicool.dao.car.CarOrderDao;
import com.jeecms.wicool.entity.car.CarOrder;

@Repository
public class CarOrderDaoImpl extends HibernateBaseDao<CarOrder, Integer>
		implements CarOrderDao {

	@Override
	protected Class<CarOrder> getEntityClass() {
		// TODO Auto-generated method stub
		return CarOrder.class;
	}
	@Override
	public Pagination getPage(String loginName,String userName,Short orderType,String orderTimeStart,String orderTimeEnd,Short status,int pageNo, int pageSize,CmsUser user) {
			Finder f = Finder.create("select bean from CarOrder bean where 1=1 ");
			SimpleDateFormat simpleTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			if(!StringUtils.isBlank(loginName)){
				f.append(" and bean.adviser.loginName = :loginName");
				f.setParam("loginName",  loginName);
			}
			if (!StringUtils.isBlank(userName)){
				f.append(" and bean.user.name like :userName");
				f.setParam("userName",  "%"+userName+"%" );
			}if (orderType!=null){
				f.append(" and bean.orderType = :orderType");
				f.setParam("orderType",orderType);
			}if (status!=null){
				f.append(" and bean.status = :status");
				f.setParam("status",status);
			}if (!StringUtils.isBlank(orderTimeStart)){
				orderTimeStart=orderTimeStart+" 00:00:00";
				try {
					Date start=simpleTime.parse(orderTimeStart);
				
				f.append(" and bean.orderTime >= :start");
				f.setParam("start",  start );
				
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}if (!StringUtils.isBlank(orderTimeEnd)){
				orderTimeEnd=orderTimeEnd+" 23:59:59";
				try {
					Date end = simpleTime.parse(orderTimeEnd);
				
				f.append(" and bean.orderTime <= :end");
				f.setParam("end",  end );
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}if(user.getShopId()!=null){
				f.append(" and bean.shop.id = :shopId");
				f.setParam("shopId",user.getShopId());
			}
			f.append(" order by bean.id desc");
			return find(f, pageNo, pageSize);
	}
	@Override
	public List<CarOrder> getCarOrderList(Short orderType,String orderTimeStart,String orderTimeEnd,CmsUser user){
		Finder f = Finder.create("select bean from CarOrder bean where 1=1 ");
		SimpleDateFormat simpleTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		if (orderType!=null){
			f.append(" and bean.orderType = :orderType");
			f.setParam("orderType",orderType);
		}if (!StringUtils.isBlank(orderTimeStart)){
			orderTimeStart=orderTimeStart+" 00:00:00";
			try {
				Date start=simpleTime.parse(orderTimeStart);
			
			f.append(" and bean.orderTime >= :start");
			f.setParam("start",  start );
			
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}if (!StringUtils.isBlank(orderTimeEnd)){
			orderTimeEnd=orderTimeEnd+" 23:59:59";
			try {
				Date end = simpleTime.parse(orderTimeEnd);
			
			f.append(" and bean.orderTime <= :end");
			f.setParam("end",  end );
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}if(user.getShopId()!=null){
			f.append(" and bean.shop.id = :shopId");
			f.setParam("shopId",user.getShopId());
		}
		f.append(" order by bean.orderTime desc");
		return find(f);
	}
	@Override
	public CarOrder findById(Integer id) {
		CarOrder entity = get(id);
		return entity;
	}
	@Override
	public CarOrder save(CarOrder bean) {
		getSession().save(bean);
		return bean;
	}
	@Override
	public CarOrder deleteById(Integer id) {
		CarOrder entity = super.get(id);
		if (entity != null) {
			getSession().delete(entity);
			getSession().flush();
		}
		return entity;
	}
	@Override
	public List<CarOrder> getAll(CmsUser user) {
		Finder f= Finder.create("select bean from CarOrder bean where 1=1 ");
		if(user.getShopId()!=null){
			f.append(" and bean.shop.id = :shopId");
			f.setParam("shopId",user.getShopId());
		}
		f.append(" order by bean.id desc");
		return find(f);
	}
}